import qrcode
import sys
from PIL import Image, ImageDraw, ImageFont


def mpix(v, dpi=300):
    return int(v * (dpi / 25.4))


if __name__ == "__main__":
    a = sys.argv[1]
    b = sys.argv[2]
    c = sys.argv[3]
    d = sys.argv[4]

    w = 100
    h = w * 0.618
    code = "T1-%s%s%s%s" % (a, b, c, d)

    color = (0, 71, 140)
    fill = (255, 255, 255)

    bg = Image.new('RGB', (mpix(w), mpix(h)), color=color)

    qr = qrcode.QRCode(box_size=16, border=2)

    fill_color = "black"
    back_color = "white"

    qr.add_data(code)
    qr.make(fit=True)
    qr_img = qr.make_image(fill_color=fill_color, back_color=back_color)

    bg_w, bg_h = bg.size
    qr_w, qr_h = qr_img.size

    q_x = bg_w - qr_w - mpix(10)
    q_y = int((bg_h - qr_h) * 3 / 4)

    bg.paste(qr_img, (q_x, q_y))

    draw = ImageDraw.Draw(bg)

    f_x = mpix(8)
    f_y = mpix(2)

    fill = "white"

    font = ImageFont.truetype("hb.ttf", size=85)
    draw.text((f_x, f_y), code, fill=fill, font=font)

    bg.save("./station/%s.png" % code)
